# @Time:   2021/2/7 17:08
# @Author: Zong Wei


from time import sleep

from faker import Faker
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as ec
from selenium.webdriver.support.wait import WebDriverWait

from ui_automation_testing.test.pageObject.loginPage import LoginPage
from ui_automation_testing.utils.logger import Logger


class BankPage(LoginPage):
    logger = Logger(__name__).get_log()
    menu = (By.XPATH, "//ul[@class='site-menu']//span[.='业务通用']")
    sub_menu = (By.XPATH, "//ul[@class='site-menu']//span[.='修改供方银行']")
    search_button = (By.ID, "lr-search")
    add_button = (By.ID, "lr-add")
    edit_button = (By.ID, "lr-edit")
    del_button = (By.ID, "lr-delete")
    save_button = (By.ID, "lr-save")
    clear_button = (By.ID, "lr-clear")

    bank_cn = (By.ID, "PartnerNameCN")
    re_code = (By.ID, "PartnerCode")
    bank = (By.ID, "m_bank")
    account = (By.ID, "m_account")
    re_acc = (By.ID, "m_relationBankNo")

    confirm_button = (By.XPATH, "//a[.='确定']")
    first_row = (By.XPATH, "//span[.='银行']//following::tr[1]")

    def __init__(self):
        self.driver = LoginPage().login()
        self.wait = WebDriverWait(self.driver, 10)

    def bank_home(self):
        self.logger.info("登录系统成功")
        self.wait.until(ec.element_to_be_clickable(self.menu)).click()
        sleep(1)
        self.driver.execute_script('$("#admui-navTabs").scrollTop(1000)')
        self.wait.until(ec.element_to_be_clickable(self.sub_menu)).click()
        sleep(1)
        return self.driver

    def search_bank(self):
        self.driver.switch_to.frame(1)
        self.wait.until(ec.presence_of_element_located(self.bank_cn)).send_keys('测试')
        self.wait.until(ec.presence_of_element_located(self.re_code)).send_keys('test')
        sleep(1)
        self.wait.until(ec.element_to_be_clickable(self.search_button)).click()
        sleep(2)
        self.logger.info("查询完成")
        self.wait.until(ec.element_to_be_clickable(self.clear_button)).click()

    def edit_bank(self):
        self.search_bank()
        self.wait.until(ec.element_to_be_clickable(self.first_row)).click()
        sleep(1)
        self.wait.until(ec.element_to_be_clickable(self.edit_button)).click()
        sleep(1)
        self.wait.until(ec.presence_of_element_located(self.bank)).clear()
        self.wait.until(ec.presence_of_element_located(self.bank)).send_keys(Faker().company())
        sleep(1)
        self.wait.until(ec.presence_of_element_located(self.account)).clear()
        self.wait.until(ec.presence_of_element_located(self.account)).send_keys(Faker().bban())
        self.wait.until(ec.presence_of_element_located(self.re_acc)).send_keys('USB')
        self.wait.until(ec.element_to_be_clickable(self.confirm_button)).click()
        self.logger.info("修改银行完成")


if __name__ == '__main__':
    pg = BankPage()
    pg.bank_home()
    pg.edit_bank()

